﻿'Imports log4net
Imports PMOutlookAddIn.UI
Imports System.Xml


Public Class ThisAddIn
    Private _pmToolBar As pmToolbar
   
    Dim WithEvents allInsp As Outlook.Inspectors
    Public allInspHelper As New InspectorHelperClass

    Private menuTag As String = "Opties"
    Private WithEvents menuCommand As Office.CommandBarButton

    'Private Shared ReadOnly _log As ILog = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)

    'Friend Shared ReadOnly Property Log() As ILog
    '    Get
    '        Return _log
    '    End Get
    'End Property

    'Private Sub InitLog()
    '    log4net.Config.XmlConfigurator.Configure()
    'End Sub

    Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
        'InitLog()
        Try
            _pmToolBar = New pmToolbar(Me.Application)

            allInsp = Globals.ThisAddIn.Application.Inspectors

            'Dim archivePromarket As Outlook.MAPIFolder = SetNewStore2("c:\archivePromarket.pst", "Archief Promarket")
         
            checkIfMenuBarExists()
            addMenuBar()


            'Me.Application.GetNamespace("mapi").CurrentUser.Address 
        Catch ex As System.Exception
            ExceptionHandler.ShowAndLog(ex.Message, ex)
        End Try
    End Sub

    Private Sub ThisAddIn_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown
        _pmToolBar = Nothing
        allInspHelper = Nothing
        allInsp = Nothing
    End Sub

    Private Sub Application_ItemSend(ByVal Item As Object, ByRef Cancel As Boolean) Handles Application.ItemSend
        Try
            Dim mail As Outlook.MailItem
            If (TypeOf Item Is Outlook.MailItem) Then
                mail = CType(Item, Outlook.MailItem)

                Using _pmCopyMailForm As pmCopyMailForm = New pmCopyMailForm(Me.Application, mail)
                    _pmCopyMailForm.ShowDialog()
                End Using
            End If
        Catch ex As System.Exception
            ExceptionHandler.ShowAndLog("Fout na kopiëren van een mail", ex)
        End Try
    End Sub


    Private Sub allInsp_NewInspector(ByVal Inspector As Microsoft.Office.Interop.Outlook.Inspector) Handles allInsp.NewInspector
        Dim itm As Object
        Dim id As String
        On Error Resume Next

        ' for the item types you want to have your CommandBar or button
        ' add the Inspector to the wrapper class collection 
        itm = Inspector.CurrentItem
        If TypeOf itm Is Outlook.MailItem Then
            id = allInspHelper.AddInsp(Inspector)
        ElseIf TypeOf itm Is Outlook.ContactItem Then
            id = allInspHelper.AddInsp(Inspector)
        End If
        itm = Nothing
    End Sub

    Private Sub checkIfMenuBarExists()
        Dim foundMenu As Office.CommandBarPopup = CType(Me.Application.ActiveExplorer.CommandBars.ActiveMenuBar.FindControl(Office.MsoControlType.msoControlPopup, System.Type.Missing, menuTag, True, True), Microsoft.Office.Core.CommandBarPopup)
        If foundMenu IsNot Nothing Then
            foundMenu.Delete()
        End If
    End Sub

    Private Sub addMenuBar()
        Dim menuBar As Office.CommandBar = Me.Application.ActiveExplorer.CommandBars.ActiveMenuBar
        Dim menuCaption As String = "Promarket"

        If menuBar IsNot Nothing Then
            Dim cmdBarControl As Office.CommandBarPopup = Nothing
            Dim controlCount As Integer = menuBar.Controls.Count

            'add the new menu
            cmdBarControl = CType(menuBar.Controls.Add(Type:=Office.MsoControlType.msoControlPopup, Before:=controlCount, Temporary:=True), Office.CommandBarPopup)

            cmdBarControl.Caption = menuCaption

            'add the menu command
            menuCommand = CType(cmdBarControl.Controls.Add(Type:=Office.MsoControlType.msoControlButton, Temporary:=True), Office.CommandBarButton)

            With menuCommand
                .Caption = "Instellingen..."
                .Tag = "newmenucommand"
            End With
        End If

        
    End Sub

    'Function SetNewStore2(ByVal strFileName As String, ByVal strDisplayName As String) As Outlook.MAPIFolder
    '    Dim objOL As Outlook.Application
    '    Dim objNS As Outlook.NameSpace
    '    Dim objFolder As Outlook.MAPIFolder
    '    Dim arr() As String
    '    Dim i As Integer
    '    On Error Resume Next

    '    objOL = Application  ' intrinsic Application object in Outlook VBA
    '    objNS = objOL.GetNamespace("MAPI")

    '    ' build array of all the information store IDs
    '    ReDim arr(objNS.Folders.Count - 1)
    '    i = 0
    '    For Each objFolder In objNS.Folders
    '        arr(i) = objFolder.EntryID
    '        i = i + 1
    '    Next
    '    objFolder = Nothing

    '    objNS.AddStore(strFileName)
    '    ' make "best guess" that new store is the last one in the collection
    '    objFolder = objNS.Folders.GetLast
    '    ' but confirm against array
    '    If FolderEntryIDIsInArray(objFolder, arr) Then
    '        ' check all top-level store folders against array
    '        '   until we find the one that doesn't have an
    '        '   EntryID in the array
    '        For i = 1 To (objNS.Folders.Count - 1)
    '            objFolder = objNS.Folders.GetPrevious
    '            If Not FolderEntryIDIsInArray(objFolder, arr) Then
    '                Exit For
    '            End If
    '        Next
    '    End If

    '    ' give the newly added PST store a display name
    '    '    This should be unique to make it easier to distinguish
    '    '    it from other stores.
    '    objFolder.Name = strDisplayName

    '    ' these statements refresh the folder name
    '    objNS.RemoveStore(objFolder)
    '    objFolder = Nothing
    '    objNS.AddStore(strFileName)

    '    ' repeat the earlier process to get the newly added store
    '    ' make "best guess" that new store is the last one in the collection
    '    objFolder = objNS.Folders.GetLast
    '    ' but confirm against array
    '    If FolderEntryIDIsInArray(objFolder, arr) Then
    '        ' check all top-level store folders against array
    '        '   until we find the one that doesn't have an
    '        '   EntryID in the array
    '        For i = 1 To (objNS.Folders.Count - 1)
    '            objFolder = objNS.Folders.GetPrevious
    '            If Not FolderEntryIDIsInArray(objFolder, arr) Then
    '                Exit For
    '            End If
    '        Next
    '    End If

    '    SetNewStore2 = objFolder

    '    objOL = Nothing
    '    objNS = Nothing
    'End Function


    'Function FolderEntryIDIsInArray(ByVal fld As Outlook.MAPIFolder, ByVal arr() As String) As Boolean
    '    Dim blnInArray As Boolean
    '    For i = 0 To UBound(arr)
    '        If arr(i) = fld.EntryID Then
    '            blnInArray = True
    '            Exit For
    '        End If
    '    Next
    '    FolderEntryIDIsInArray = blnInArray
    'End Function

    Private Sub menuCommand_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Handles menuCommand.Click
        Dim pmSet As New pmSettings
        pmSet.ShowDialog()
    End Sub


End Class

